package com.jasonsang.rsocket_service;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.var;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.time.Instant;
import java.util.function.Supplier;
import java.util.stream.Stream;

@SpringBootApplication
public class RsocketServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(RsocketServiceApplication.class, args);
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    class RequestCallBack {
        private String message;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    class ResponseCallBack {
        private String name;
    }


    @Controller
    class TestController {
        /**
         * fire -and forget
         * request response
         * request stream
         * channel
         * <p>
         * 已经连接不需要重复协商（不用担心重置）
         *
         * @return Mono
         */
        @MessageMapping("seek")
        Flux<ResponseCallBack> hello(RequestCallBack request) {
            //响应式流
            var stream = Stream.generate(new Supplier<ResponseCallBack>() {


                @Override
                public ResponseCallBack get() {
                    return new ResponseCallBack("你好"+request.getMessage()+"@"+ Instant.now());
                }
            });
            return Flux.fromStream(stream).delayElements(Duration.ofSeconds(1));
        }
    }
}
